package com.estore.action;

import java.util.List;
import java.util.Map;

import com.estore.converter.MD5;
import com.estore.entity.Role;
import com.estore.info.AccountInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.estore.manager.ProfileManager;
import com.estore.model.LoginModel;

public class LogInAction extends ActionSupport implements
		ModelDriven<LoginModel> {
	private static final long serialVersionUID = -7769137946345407512L;
	private LoginModel model = new LoginModel();
	private ProfileManager profileManager;

	public String login() {
		// List<ShowProfileInfo> listinfo = managerList.getListProfileInfo();
		AccountInfo infoAcc = profileManager.getAccountInfo(model.getId(),
				MD5.encryptMD5(model.getPassword()));
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (infoAcc != null) {
			Role role = profileManager.getRole(infoAcc.getUserID());
			
			session.put("logged-in", "true");
			session.put("role", role.getRole());
			session.put("username", model.getId());
			return "success";
		} else {
			model.setErrorLogin("Sai Tài Khoản Hoặc Mật Khẩu");
			model.setId(null);
			return "error";
		}
	}

	public String logout() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.remove("logged-in");
		session.remove("username");
		session.remove("role");
		return "success";

	}

	@Override
	public LoginModel getModel() {
		return model;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public void setModel(LoginModel model) {
		this.model = model;
	}

	public ProfileManager getProfileManager() {
		return profileManager;
	}

	public void setProfileManager(ProfileManager profileManager) {
		this.profileManager = profileManager;
	}

}
